function [isabs, abspath] = isabsolute(tpath)
% isabsolute  - returns true if the given path is absolute
%
% FORMAT:       isabs = isabsolute(testpath)
%
% Input fields:
%
%       testpath    string to test for absolute path content
%
% Output fields:
%
%       isabs       true for absolute paths, false for relative
%
% See also EXIST

% Version:  v0.7f
% Build:    8110521
% Date:     Nov-05 2008, 9:00 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 1 || ...
   ~ischar(tpath) || ...
    isempty(tpath)
    error( ...
        'BVQXtools:BadArgument', ...
        'One non-empty 1xN char path argument is required.' ...
    );
end

% make valid char argument
tpath = tpath(:)';

% get machine ostype
try
    machine = ostype('machine');
    machine = machine.machine;
catch
    if ispc
        machine = 'WIN';
    else
        machine = 'UNIX';
    end
end

% default is false
isabs = false;
abspath = tpath;

% machine dependent code
switch (machine)

    % for PCs/Windows hosts
    case {'WIN'}

        % for now, replace \'s with /'s
        tpath = strrep(tpath, '\', '/');
        % path can only be absolute for either of
        % - X:\<...>
        % - \\HOST\PATH_TO_UNC
        if (numel(tpath) > 2 && ...
            tpath(1) > 64 && ...
            tpath(1) < 123 && ...
            (tpath(1) < 91 || tpath(1) > 96) && ...
            strcmp(tpath(2:3), ':/')) || ...
           (numel(tpath) > 3 && ...
            strcmp(tpath(1:2), '//'))
            isabs = true;
        elseif tpath(1) == '/'
            pwdpath = cd; % see dbtype pwd
            abspath = [pwdpath(1) ':' tpath];
        else
            abspath = strrep([pwd filesep tpath], '//', '/');
        end

    % for Linux/Unix'ish systems
    case {'UNIX'}

        % path can only be "absolute" for either of
        % - /...
        % - ~/...
        if tpath(1) == '/' || ...
            (numel(tpath) > 1 && ...
             strcmp(tpath(1:2), '~/'))
            isabs = true;
        else
            abspath = strrep([pwd filesep tpath], '//', '/');
        end

    % for Macintosh
    case {'MAC'}

        % path could be absolute for either of
        % - /...
        % - ~/...
        if tpath(1) == '/' || ...
            (numel(tpath) > 1 && ...
             strcmp(tpath(1:2), '~/'))
            isabs = true;
        else
            abspath = strrep([pwd filesep tpath], '//', '/');
        end
end
